package com.axcessfinancial.common;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map.Entry;

import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class MailUtil {

	private MailSender mailSender;
	private SimpleMailMessage templateMessage;

	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void setTemplateMessage(SimpleMailMessage templateMessage) {
		this.templateMessage = templateMessage;
	}

	public void sendMail(Entry<String, String> serverData, int status,
			String statusReason) {
		SimpleMailMessage message = new SimpleMailMessage(this.templateMessage);

		String server = serverData.getKey();
		String url = serverData.getValue();

		SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyy HH:mm:ss");
		String date = format.format(new Date());

		message.setSubject(String.format(message.getSubject(), server));
		message.setText(String.format(message.getText(), server, url, status,
				statusReason, date));
		mailSender.send(message);
	}
	
	public void sendMail(Entry<String, String> serverData, int status) {
		String statusReason = "";
		if (status == ConnectionStatus.CONNECTION_REFUSED) {
			statusReason = "Connection refused";
		}
		sendMail(serverData, status, statusReason);
	}

}
